package com.hbx.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbx.eduservice.client.VodClient;
import com.hbx.eduservice.entity.EduCourse;
import com.hbx.eduservice.entity.EduVideo;
import com.hbx.eduservice.mapper.EduVideoMapper;
import com.hbx.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author hbx
 * @since 2023-10-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;
    //删除小节还有视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {
        //根据课程id查询所有视频id
        LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduVideo::getCourseId,courseId);
        wrapper.select(EduVideo::getVideoSourceId);
        List<EduVideo> list = this.list(wrapper);

        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i) != null){
                String videoSourceId = list.get(i).getVideoSourceId();
                arrayList.add(videoSourceId);
            }
        }
        if(arrayList.size() > 0){
            vodClient.deleteBatch(arrayList);
        }

        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getCourseId,courseId);
        this.remove(queryWrapper);
    }
}
